package dunshan.textdemo

import io.gatling.core.Predef._
import io.gatling.core.session
import io.gatling.http.Predef._


class ParamDdunshan extends Simulation {

  val httpConf = http.baseUrl("http://127.0.0.1:8080")

  val csvFeeder = csv("loginName.csv").random

  //注意这里,设置提交内容type
  val contentType = Map("Content-Type" -> "application/x-www-form-urlencoded")
  //声明scenario
  val scn = scenario("formScenario").feed(csvFeeder).during(2) {
    exec(http("form_test") //http 请求name)
      .post("/login") //post地址, 真正发起的地址会拼上上面的baseUrl http://127.0.0.1:8080/login
      .headers(contentType)
      .formParam("userName", "${name}") //form 表单的
      .formParam("passWord", "123456")
      .check(jsonPath("$.code").is("0"))
      .check(jsonPath("$.data.token").exists.saveAs("token"))
    )
      // 取出 token 值并修改
      .exec(session => {
        val token = session("token").as[String]
        val token2 = "Bearer " + token
        session.set("token", token2)
      })

//       打印 session 验证正确性
            .exec { session =>
              println(session)
              session
            }

      .exec(http("7d_index").get("/token").formParam("token", "${token2}"))
  }


  setUp(scn.inject(atOnceUsers(1))).protocols(httpConf)
  //  setUp(scn.inject(rampUsers(10).during(10))).protocols(httpConf)

}
